package it.antonio.ilpizze.db.core.perst;

import it.antonio.ilpizze.db.index.utils.FPerst;
import it.antonio.ilpizze.db.io.IOFile;

public class StrPerst implements FPerst<String> {
	@Override
	public long space() {
		return 50;
	}

	@Override
	public void persist(IOFile file, String value) {
		// System.out.println("1_WRITE: " + file.position());
		long toIncrement = file.position() + 50;
		if (value == null) {
			file.writeString("NULL_STRING_VALUE");

		} else {
			if (value.length() > 48)
				throw new RuntimeException("String too long");
			file.writeString(value);
		}
		file.position(toIncrement - 1);
		file.writeBoolean(true);
	}

	@Override
	public String read(IOFile file) {
		long toIncrement = file.position() + 50;
		String value = file.readString();
		file.position(toIncrement);
		if ("NULL_STRING_VALUE".equals(value))
			return null;
		return value;
	}

}